<script setup lang="ts">
/**
 * @see {@link https://vuejs.org/api/sfc-script-setup.html#defineoptions}
 */
import {
  postOrderList,
  confirmRecharge,
  orderRead
} from "@/api/order";
import { orderQuery, listInfo } from "@/api/order/types";
import { ElMessage, ElMessageBox } from 'element-plus'
import { formatTimestamp } from '@/utils/index'
import { useStorage } from "@vueuse/core";
import { useUserStore } from "@/store/modules/user";

const store = useUserStore()
const queryFormRef = ref(ElForm); // 查询表单
const loading = ref(false);
const ids = ref([]);
const total = ref(0);
const dialog = reactive<DialogOption>({
  visible: false,
});

const queryParams = reactive<orderQuery>({
  page: 1,
  ps: 10,
  scene: '1',
});

const dataList = ref<listInfo[]>();

let rechargeTotal = useStorage("rechargeTotal", 0);
/**
 * 查询
 */
function handleQuery() {
  loading.value = true;
  postOrderList(queryParams)
    .then(({ data }) => {
      dataList.value = data.data;
      total.value = data.total;
      rechargeTotal.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  handleQuery();
}


const handReadMessages = (id: number) => {
  orderRead({ id: id }).then((res) => {
    store.rechargePageTotal()
    ElMessage({
      type: 'success',
      message: '操作成功',
    })
  }).finally(() => {
    handleQuery();
  })
}

// 确认收款
const openDialog = (id: number) => {
  ElMessageBox.prompt('确定收款吗？', '确定收款', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputErrorMessage: '输入收款金额',
    inputPattern: /\S/,
  }).then((val) => {
    confirmRecharge({ id, confirm_amount: val.value }).then((res: any) => {
      ElMessage({
        type: 'success',
        message: res.msg,
      })
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消',
    })
  })
}

onMounted(() => {
  handleQuery(); // 初始化用户列表数据
});
</script>

<template>
  <div class="app-container">
    <el-row :gutter="20">

      <el-col :lg="24" :xs="24">
        <div class="search-container">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="会员ID" prop="uid">
              <el-input v-model="queryParams.uid" placeholder="输入会员ID" clearable style="width: 200px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="订单编号" prop="s_code">
              <el-input v-model="queryParams.s_code" placeholder="输入订单编号" clearable style="width: 200px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
              <el-button @click="resetQuery">
                <i-ep-refresh />
                重置</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never">
          <el-table v-loading="loading" :data="dataList">
            <el-table-column label="编号" align="center" prop="id" width="100" />
            <el-table-column label="用户id" align="center" prop="uid" />
            <el-table-column label="订单号" align="center" prop="code" width="170" />
            <el-table-column label="订单金额" align="center" prop="amount" />
            <el-table-column label="状态" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.is_read === 1 || scope.row.is_read === 0" type="danger">未读</el-tag>
                <el-tag v-if="scope.row.is_read === 2" type="primary">已读</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="下单时间" align="center" prop="created_at">
              <template #default="scope">
                {{ formatTimestamp(scope.row.created_at * 1000) }}
              </template>
            </el-table-column>
            <el-table-column label="充值时间" align="center">
              <template #default="scope">
                {{ formatTimestamp(scope.row.updated_at * 1000) }}
              </template>
            </el-table-column>
            <!-- <el-table-column label="总收益" align="center" prop="update_time" /> -->

            <el-table-column fixed="right" align="center" label="操作" width="220">
              <template #default="scope">
                <el-button v-if="scope.row.pay_status === 1" type="primary" link size="small"
                  @click.stop="openDialog(scope.row.id)">确定收款</el-button>
                <el-button v-if="scope.row.is_read === 1 || scope.row.is_read === 0" type="primary" link size="small"
                  @click.stop="handReadMessages(scope.row.id)">已读</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.page"
            v-model:limit="queryParams.ps" @pagination="handleQuery" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
